// pages/label/index/index.js
import {
    baseUrl,
    isHaveLogin,
    isHaveToken
} from "../../../utils/util";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        comment: [], //评论
        labelId: "", //帖子id
        labelDetail: {}, //帖子详情
        userInfo: {}, //用户信息
        imageList: [], //图片列表
        Login: false, //是否登录
        // fabButton: {
        //     icon: 'chat',
        //     variant: 'outline',
        //     openType: 'getPhoneNumber',
        // },
        commentInput: '',
        dialogKey: '',
        showInput: "",
        showWithInput: false,
        showTextAndTitleWithInput: false,
    },
    // 父子传参
    handleItemChange(e) {
        console.log("handleItemChange", e)
    },

    handleClick(e) {
        console.log("handleClick", e.detail.value);
        this.setData({
            commentInput: e.detail.value
        })
    },
    // 是否登录


    // 添加评论弹窗
    showDialog(e) {
        console.log(e)
        const {
            key
        } = e.currentTarget.dataset;
        this.setData({
            [key]: true,
            dialogKey: key,
            commentInput: ""
        });
    },

    closeDialog(e) {
        console.log(e)
        if (e.type == "confirm") {
            console.log("comment", this.data.commentInput)
            this.uploadComment()
        }
        const {
            dialogKey
        } = this.data;
        this.setData({
            [dialogKey]: false,
            commentInput: ""
        });
        console.log("关闭弹窗")
    },

    uploadComment() {
        // comment=this.data.commentInput
        // console.log(comment)
        var userInfo = wx.getStorageSync('userInfo')
        var comment = this.data.commentInput
        if (comment == "") {
            wx.showToast({
                title: '没有输入内容',
            })
        } else {
            var that = this
            wx.request({
                url: `${baseUrl}/label/comment/addComment`,
                method: 'POST',
                data: {
                    "comment": this.data.commentInput,
                    "labelId": this.data.labelId,
                    "userId": userInfo.id
                },
                success(res) {
                    console.log("uploadComment", res)
                    if (res.data.code === 20000) {
                        that.getLabelDetail()
                    } else {
                        wx.showToast({
                            title: '发表失败',
                            duration: 1000,
                            icon: 'error',
                        })
                    }

                }
            })
        }
        // console.log(userInfo)
        // comment=this.data.commentInput
        // console.log("labelId",this.data.labelId)

    },

    getLabelDetail() {
        var that = this
        wx.request({
            url: baseUrl + `/label/getLoabelById/${this.data.labelId}`,
            success(res) {
                console.log("labelDetail res", res)
                that.setData({
                    comment: res.data.data.comment,
                    labelDetail: res.data.data.index,
                    userInfo: res.data.data.userinfo
                })
            }
        })
    },
    getLabelImage() {
        var that = this
        wx.request({
            url: baseUrl + `/label/img/getImgList/${this.data.labelId}`,
            success(res) {
                console.log("image list ", res)
                that.setData({
                    imageList: res.data.data.imageList
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log("detail option", options)
        this.data.labelId = options.id
        // var that = this
        if (isHaveLogin()) {
            this.setData({
                login: true
            })
        }
        this.getLabelDetail()
        this.getLabelImage()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})